
"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.isAbsoluteURL = exports.combineURLs = exports.isEmptyObject = exports.deepMerge = exports.merge = exports.extend = exports.bind = exports.throttle = exports.debounce = exports.Storage = exports.pageData = exports.sleep = exports.isIdCard = exports.isEmail = exports.isPhoneNumber = exports.transformQueryString = exports.curEnv = exports.rand = exports.formatPhoneNumber = exports.transformURL = exports.formatEveryDay = exports.formatDate = exports.prefixZero = exports.wxJsPay = void 0;

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var wxJsPay = function wxJsPay(payParams) {
    var callBack = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    WeixinJSBridge.invoke("getBrandWCPayRequest", payParams, function (_ref) {
        var err_msg = _ref.err_msg;
        return callBack.complete(), err_msg == "get_brand_wcpay_request:ok" ? uni.showToast({
            title: "支付成功",
            complete: callBack.success
        }) : uni.showToast({
            title: "支付失败",
            icon: "none",
            complete: callBack.fail
        });
    });
};

exports.wxJsPay = wxJsPay;

var prefixZero = function prefixZero(n) {
    return String(+n > 9 ? n : "0".concat(n));
};

exports.prefixZero = prefixZero;

var formatDate = function formatDate() {
    var format = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "Y-M-D h:m:s";
    var timestamp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Date.now();

    /**
     * @method prefixZero
     * @param {timestamp} number
     * @param {format} string
     * @return {string}
     */
    var date = new Date(timestamp);
    var time = {
        Y: date.getFullYear(),
        M: date.getMonth() + 1,
        D: date.getDate(),
        W: date.getDay(),
        h: date.getHours(),
        m: date.getMinutes(),
        s: date.getSeconds()
    };
    return format.replace("Y", String(time.Y)).replace("M", prefixZero(time.M)).replace("D", prefixZero(time.D)).replace("W", String(time.W)).replace("h", prefixZero(time.h)).replace("m", prefixZero(time.m)).replace("s", prefixZero(time.s));
};
/**
* @param { Date } start
* @param { Date } end
* @returns { Array<string> }
*/


exports.formatDate = formatDate;

var formatEveryDay = function formatEveryDay(start, end) {
    var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
    var dateList = [];

    while (end.getTime() - start.getTime() >= 0) {
        dateList.push(formatDate(format, start.getTime()));
        start.setDate(start.getDate() + 1);
    }

    return dateList;
};

exports.formatEveryDay = formatEveryDay;

var transformURL = function transformURL(url, params, hash) {
    var paramsArr = Object.keys(params).map(function (key) {
        return "".concat(key, "=").concat(params[key]);
    });
    return encodeURIComponent("".concat(url, "?").concat(paramsArr.join("&")).concat(hash ? "#" + hash : ""));
};

exports.transformURL = transformURL;

var formatPhoneNumber = function formatPhoneNumber(phoneNumber) {
    return phoneNumber.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
};

exports.formatPhoneNumber = formatPhoneNumber;

var rand = function rand(min, max) {
    return ~~(Math.random() * (max - min + 1) + min);
};

exports.rand = rand;

var curEnv = function curEnv() {
    var curEnv;
    return curEnv;
};

exports.curEnv = curEnv;

var transformQueryString = function transformQueryString(params) {
    if (typeof params === "string") {
        var queryStrings = decodeURIComponent(params).match(/[^?&]+=[^?&]+/g);
        return queryStrings ? Object.fromEntries(queryStrings.map(function (item) {
            return item.split(/^([^=]*)=*/).filter(function (item) {
                return item;
            });
        })) : Object();
    }

    if (_typeof(params) === "object") {
        return Object.keys(params).filter(function (key) {
            return params[key];
        }).map(function (key) {
            return "".concat(key, "=").concat(params[key]);
        }).join("&");
    }

    throw Error("Parameter error");
};

exports.transformQueryString = transformQueryString;

var isPhoneNumber = function isPhoneNumber(str) {
    return /^1[0-9]{10}$/.test(str);
};

exports.isPhoneNumber = isPhoneNumber;

var isEmail = function isEmail(str) {
    return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(str);
};

exports.isEmail = isEmail;

var isIdCard = function isIdCard(str) {
    return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(str);
};

exports.isIdCard = isIdCard;

var sleep = function sleep(second) {
    return new Promise(function (resolve) {
        return setTimeout(resolve, second * 1000);
    });
};

exports.sleep = sleep;

var pageData = function pageData() {
    var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -2;
    var pages = getCurrentPages();
    return pages[pages.length + page];
};
/**
 * @var {object} Storage 设置 获取 删除 清空
 * @property {function} set
 * @property {function} remove
 * @property {function} get
 * @property {function} clear
 */


exports.pageData = pageData;
var Storage = {
    set: function set(key, value) {
        var validityDay = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

        var _ref2 = uni.getStorageSync(key) || {},
            oldValue = _ref2.value,
            storageCreate = _ref2.createTime,
            storageExpire = _ref2.expireTime,
            createTime = storageCreate || Date.now();

        uni.setStorageSync(key, Object.prototype.toString.call(oldValue) === "[object Object]" ? {
            value: Object.assign(oldValue, value),
            createTime: createTime,
            expireTime: validityDay ? createTime + validityDay * 86400000 : storageExpire
        } : {
            value: value,
            createTime: createTime,
            expireTime: validityDay && createTime + validityDay * 86400000
        });
        return value;
    },
    get: function get(key) {
        var _uni$getStorageSync = uni.getStorageSync(key),
            expireTime = _uni$getStorageSync.expireTime,
            value = _uni$getStorageSync.value;

        return expireTime && Date.now() >= expireTime ? this.remove(key) : value;
    },
    remove: function remove() {
        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
        }

        args.forEach(function (key) {
            return uni.removeStorageSync(key);
        });
    },
    clear: function clear() {
        uni.clearStorageSync();
    }
}; // 防抖  

exports.Storage = Storage;

var debounce = function debounce(fn) {
    var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
    var isImmediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var timer = 0,
        flag = true;
    return {
        exec: function exec() {
            for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
                args[_key2] = arguments[_key2];
            }

            timer && clearTimeout(timer);

            if (flag) {
                isImmediate && fn(args);
                flag = false;
            }

            timer = setTimeout(function () {
                return isImmediate ? flag = true : fn(args);
            }, delay);
        }
    };
}; // 节流  


exports.debounce = debounce;

var throttle = function throttle(fn) {
    var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
    var isImmediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var flag = true;
    return function () {
        for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
            args[_key3] = arguments[_key3];
        }

        if (flag) {
            isImmediate && fn(args);
            flag = false;
            setTimeout(function () {
                !isImmediate && fn(args);
                flag = true;
            }, delay);
        }
    };
};

exports.throttle = throttle;

var bind = function bind(fn, thisArg) {
    return function () {
        for (var _len4 = arguments.length, arg = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
            arg[_key4] = arguments[_key4];
        }

        return fn.apply(thisArg, arg);
    };
};

exports.bind = bind;

var extend = function extend(a, b, thisArg) {
    var o = Object.getOwnPropertyNames(b);
    o.forEach(function (attr) {
        if (thisArg && typeof b[attr] === "function") {
            a[attr] = bind(b[attr], thisArg);
        } else {
            a[attr] = b[attr];
        }
    });
    return a;
};

exports.extend = extend;

var merge = function merge() {
    var result = {};

    for (var _len5 = arguments.length, arg = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
        arg[_key5] = arguments[_key5];
    }

    arg.forEach(function (e) {
        for (var key in e) {
            if (_typeof(e[key]) === "object" && !isEmptyObject(e[key])) {
                merge(result[key], e[key]);
            }

            result[key] = e[key];
        }
    });
    return result;
};

exports.merge = merge;

var deepMerge = function deepMerge() {
    var result = {};

    for (var _len6 = arguments.length, arg = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
        arg[_key6] = arguments[_key6];
    }

    arg.forEach(function (e) {
        if (e && _typeof(e) === "object" && !isEmptyObject(e)) {
            Object.keys(e).forEach(function (key) {
                if (_typeof(e[key]) === "object") {
                    return result[key] = deepMerge(result[key], e[key]);
                }

                result[key] = e[key];
            });
        }
    });
    return result;
};

exports.deepMerge = deepMerge;

var isEmptyObject = function isEmptyObject(obj) {
    return obj === null || Object.getOwnPropertyNames(obj).length === 0;
};

exports.isEmptyObject = isEmptyObject;

var combineURLs = function combineURLs(baseURL, relativeURL) {
    return relativeURL ? baseURL.replace(/\/+$/, "") + "/" + relativeURL.replace(/^\/+/, "") : baseURL;
};
/**
 * Determines whether the specified URL is absolute
 *
 * @param {string} url The URL to test
 * @returns {boolean} True if the specified URL is absolute, otherwise false
 */


exports.combineURLs = combineURLs;

var isAbsoluteURL = function isAbsoluteURL(url) {
    return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
};

exports.isAbsoluteURL = isAbsoluteURL;